package com.noovo.comport.server;

import java.util.ArrayList;
import android.content.UriMatcher;
import android_serialport_api.SerialPortFinder;

public class ComInfo {
	public final static String TAG = "ComPort4";
	public final static boolean DEBUG_ON = true;
	public static final int DEFAULT_RATE = 9600;
	public static final int DEFAULT_DELAY = 500;
	public static final int HANDLER_CLOSE = 0;
	public static final int HANDLER_OPEN = 1;
	public static final int HANDLER_SEND = 2;
	public static final int HANDLER_SEND_STOP = 3;
	public static final int HANDLER_DESTROY = 4;
	//HANDLER_SERVICE  1-4 is Com data Receive finish used
	public static final int HANDLER_SERVICE_RECEIVE = 99;
	public static final int HANDLER_SERVICE_ERROR = 98;
	public static final int HANDLER_SERVICE_COM_DATA_RECEIVE = 97;
	//HANDLER_SERVICE END
	public static final int CONNECT_TRY_MAX = 10;
	public static final int CONNECT_TRY_DELSY = 4000;
	public static final String PORT_1_1 = "/dev/ttyACM0";
	public static final String PORT_1_2 = "/dev/ttyUSB7";
	public static final String PORT_2_1 = "/dev/ttyACM1";
	public static final String PORT_2_2 = "/dev/ttyUSB8";
	public static final String PORT_3_1 = "/dev/ttyACM2";
	public static final String PORT_3_2 = "/dev/ttyUSB9";
	public static final String PORT_4_1 = "/dev/ttyS1";
	public static final String AUTHORITY = "com.noovo.comport.setting";
	public static final String DB_ITEM_PORT = "port";
	public static final String DB_ITEM_NAME = "name";
	public static final String DB_ITEM_RATE = "rate";
	public static final String DB_ITEM_DATABITS = "databits";
	public static final String DB_ITEM_STOPBITS = "stopbits";
	public static final String DB_ITEM_PARITY = "parity";
	public static final String DB_ITEM_RUN = "run";
	public static final String DB_ITEM_LOOP = "loop";
	public static final String DB_ITEM_DELAY = "delay";
	public static final String DB_ITEM_MSG = "msg";
	public static final String DB_ITEM_SEND = "send";
	public static final String DB_ITEM_RECEIVE = "receive";
	public static final String DB_ITEM_RECEIVE_ID = "id";
	public static final int DB_ITEM_RECEIVE_MAX = 20;
	public static final int TYPE_PORT_IN_SET = 0;
//	public static final int TYPE_PORT_IN_RUN = 1;
	public static final int TYPE_PORT_IN_SEND = 2;
	public static final int TYPE_PORT_OUT_RECEIVE = 3;
	public static final UriMatcher uriMatcher;
	static {
		uriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
		uriMatcher.addURI(ComInfo.AUTHORITY, "port_in/set/#", TYPE_PORT_IN_SET);
//		uriMatcher.addURI(ComInfo.AUTHORITY, "port_in/run/#", TYPE_PORT_IN_RUN);
		uriMatcher.addURI(ComInfo.AUTHORITY, "port_in/send/#", TYPE_PORT_IN_SEND);
		uriMatcher.addURI(ComInfo.AUTHORITY, "port_out/receive/#", TYPE_PORT_OUT_RECEIVE);
		uriMatcher.addURI(ComInfo.AUTHORITY, "port_out/receive/#/#", TYPE_PORT_OUT_RECEIVE);
	}
	private static ArrayList<String> ports;

	public static String getPort(int port) {
		if (ports == null) {
			ports = new ArrayList<String>();
			String[] entryValues = new SerialPortFinder().getAllDevicesPath();
			for (int i = 0; i < entryValues.length; i++) {
				ports.add(entryValues[i]);
			}
		}
		switch (port) {
		case 1:
			if (ports.contains(PORT_1_1)) {
				return PORT_1_1;
			} else if (ports.contains(PORT_1_2)) {
				return PORT_1_2;
			}
			break;
		case 2:
			if (ports.contains(PORT_2_1)) {
				return PORT_2_1;
			} else if (ports.contains(PORT_2_2)) {
				return PORT_2_2;
			}
			break;
		case 3:
			if (ports.contains(PORT_3_1)) {
				return PORT_3_1;
			} else if (ports.contains(PORT_3_2)) {
				return PORT_3_2;
			}
			break;
		case 4:
			if (ports.contains(PORT_4_1)) {
				return PORT_4_1;
			}
			break;
		}
		return null;
	}
}
